package com.cndw.kungfu.model.player;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.dao.FriendDao;
import com.cndw.kungfu.domain.Friend;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;

@Component
public class FriendManager {
	@Autowired
	private FriendDao dao;
	
	/**
	 * 获取好友列表
	 * @param playerId
	 * @return
	 */
	public List<Friend> getFriend(long playerId){
		try {
			return dao.select(playerId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 获取分组名称
	 * @param playerId
	 * @return
	 */
	public String[] getGroupNames(String names){
		if (names == null || names.length() <= 0){//初始分组
			names = LanguageLib.get("friend.group.name");
		}		
		if (names!= null && names.length() > 0) {
			String[] groupNames = names.split(Constants.COMMSPLIT);
			return groupNames;
		}

		return null;
	}
	
	/**
	 * 添加好友
	 * @param playerId
	 * @param friendId
	 * @param groupId
	 * @return
	 */
	public Friend addFriend(long playerId,long friendId,String friendName,int vocationId,int groupId){
		Friend friend = new Friend();
		friend.setPlayerId(playerId);
		friend.setFriendId(friendId);
		friend.setFriendName(friendName);
		friend.setVocationId(vocationId);
		friend.setGroupId(groupId);
		friend.setAddTime(TimeProcesser.getUnixTime());
		
		try {
			int id = dao.insert(friend).intValue();
			friend.setId(id);
			return friend;
		} catch (Exception e) {
			e.printStackTrace();			
		}
		return null;
		
	}
	
	public boolean updateFriend(Friend friend){
		try {
			dao.update(friend);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public void deleteFriend(long playerId,long friendId){
		try {
			dao.delete(playerId,friendId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateFriendName(long frindeId,String nameNew){
		try {
			dao.updateName(frindeId,nameNew);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
